home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / timedate < prev    next >
Encoding:
Text File  |  2007-04-04  |  5.0 KB  |  186 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '0.1'
  24. __title__ = 'Time/Date Utility'
  25. __doc__ = "Set the time and date on an HP Officejet."
  26.  
  27. # Std Lib
  28. import sys
  29. import re
  30. import getopt
  31. import struct
  32.  
  33. # Local
  34. from base.g import *
  35. from base.codes import *
  36. from base import device, status, utils, pml
  37. from prnt import cups
  38. from fax import fax
  39.  
  40. USAGE = [(__doc__, "", "name", True),
  41.          ("Usage: timedate.py [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  42.          utils.USAGE_ARGS,
  43.          utils.USAGE_DEVICE,
  44.          utils.USAGE_PRINTER,
  45.          utils.USAGE_SPACE,
  46.          utils.USAGE_OPTIONS,
  47.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  48.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2,
  49.          utils.USAGE_HELP,
  50.          utils.USAGE_SPACE,
  51.          utils.USAGE_NOTES,
  52.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  53.         ]
  54.  
  55. def usage(typ='text'):
  56.     if typ == 'text':
  57.         utils.log_title(__title__, __version__)
  58.  
  59.     utils.format_text(USAGE, typ, __title__, 'timedate.py', __version__)
  60.     sys.exit(0)
  61.  
  62.  
  63. PML_ERROR_CODES = {
  64.     pml.ERROR_OK_END_OF_SUPPORTED_OBJECTS: "OK: End of supported objects",
  65.     pml.ERROR_OK_NEAREST_LEGAL_VALUE_SUBSITUTED: "OK: Nearest legal value substituted",
  66.     pml.ERROR_UNKNOWN_REQUEST: "Unknown request",
  67.     pml.ERROR_BUFFER_OVERFLOW: "Buffer overflow",
  68.     pml.ERROR_COMMAND_EXECUTION: "Command execution",
  69.     pml.ERROR_UNKNOWN_OID: "Unknown OID",
  70.     pml.ERROR_OBJ_DOES_NOT_SUPPORT_SPECIFIED_ACTION: "Object does not support action",
  71.     pml.ERROR_INVALID_OR_UNSUPPORTED_VALUE: "Invalid or unsupported value",
  72.     pml.ERROR_PAST_END_OF_SUPPORTED_OBJS: "Past end of supported objects",
  73.     pml.ERROR_ACTION_CANNOT_BE_PERFORMED_NOW: "Action cannot be performed now",
  74.     pml.ERROR_SYNTAX: "Syntax",
  75. }
  76.  
  77.  
  78.  
  79. try:
  80.     opts, args = getopt.getopt(sys.argv[1:],
  81.                                 'p:d:hl:b:g',
  82.                                 ['printer=',
  83.                                   'device=',
  84.                                   'help',
  85.                                   'help-rest',
  86.                                   'help-man',
  87.                                   'help-desc',
  88.                                   'logging=',
  89.                                   'bus=',
  90.                                 ]
  91.                               )
  92. except getopt.GetoptError:
  93.     usage()
  94.  
  95. printer_name = None
  96. device_uri = None
  97. bus = device.DEFAULT_PROBE_BUS
  98. log_level = logger.DEFAULT_LOG_LEVEL
  99.  
  100. if os.getenv("HPLIP_DEBUG"):
  101.     log.set_level('debug')
  102.  
  103. for o, a in opts:
  104.     if o in ('-h', '--help'):
  105.         usage()
  106.  
  107.     elif o == '--help-rest':
  108.         usage('rest')
  109.  
  110.     elif o == '--help-man':
  111.         usage('man')
  112.  
  113.     elif o == '--help-desc':
  114.         print __doc__,
  115.         sys.exit(0)
  116.  
  117.     elif o in ('-p', '--printer'):
  118.         if a.startswith('*'):
  119.             printer_name = cups.getDefault()
  120.         else:
  121.             printer_name = a
  122.  
  123.     elif o in ('-d', '--device'):
  124.         device_uri = a
  125.  
  126.     elif o in ('-b', '--bus'):
  127.         bus = a.lower().strip()
  128.         if not device.validateBusList(bus):
  129.             usage()
  130.  
  131.     elif o in ('-l', '--logging'):
  132.         log_level = a.lower().strip()
  133.         if not log.set_level(log_level):
  134.             usage()
  135.  
  136.     elif o == '-g':
  137.         log.set_level('debug')
  138.  
  139.     elif o in ('-v', '--value'):
  140.         print a
  141.  
  142.  
  143. if device_uri and printer_name:
  144.     log.error("You may not specify both a printer (-p) and a device (-d).")
  145.     usage()
  146.  
  147. utils.log_title(__title__, __version__)
  148.  
  149. if not device_uri and not printer_name:
  150.     try:
  151.         device_uri = device.getInteractiveDeviceURI(bus)
  152.         if device_uri is None:
  153.             sys.exit(1)
  154.     except Error:
  155.         log.error("Error occured during interactive mode. Exiting.")
  156.         sys.exit(0)
  157.  
  158. d = fax.FaxDevice(device_uri, printer_name)
  159.  
  160. if d.device_uri is None and printer_name:
  161.     log.error("Printer '%s' not found." % printer_name)
  162.     sys.exit(1)
  163.  
  164. if d.device_uri is None and device_uri:
  165.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  166.     sys.exit(1)
  167.  
  168. user_cfg.last_used.device_uri = d.device_uri
  169.  
  170. try:
  171.     d.open()
  172. except Error:
  173.     log.error("Unable to open device. Exiting. ")
  174.     sys.exit(1)
  175.  
  176. try:
  177.     d.setDateAndTime()
  178. except Error:
  179.     log.error("An error occured!")
  180.  
  181.  
  182. log.info("")
  183. d.close()
  184. log.info('Done.')
  185. sys.exit(0)
  186.